<?php

class login {

    private $user_id;
    private $grouptype;
    private $fullname;
    private $queryExecutor;

    public function login() {
        $this->queryExecutor = new QueryExecutor();
    }

    public function close() {
        $this->queryExecutor->close();
    }

    function getuser_id() {
        return $this->user_id;
    }

    function getgrouptype() {
        return $this->grouptype;
    }

    function getfullname() {
        return $this->fullname;
    }

    private function checkgroup($group_id) {
        $sql = "SELECT  group_id,group_name,group_type  FROM sme_group WHERE group_id='$group_id'";
        $record = $this->queryExecutor->execute($sql);
        if (count($record) == 1) {
            $field = $record[0];
            if ($field[2] == '1') {
                $this->grouptype = $field[1];
                return TRUE;
            } else {
                return FALSE;
            }
        } else {
            return FALSE;
        }
    }

    function checkrecord($email, $password) {
        $encodeEmail = base64_encode($email);
        $encodePassword = sha1($password);
        $sql = "SELECT user_id ,group_id,name,lastname FROM sme_user WHERE email='$encodeEmail' AND password='$encodePassword' AND active='1' AND deleted='0'";
        $record = $this->queryExecutor->execute($sql);
        if (count($record) == 1) {
            $field = $record[0];
            if ($this->checkgroup($field[1])) {
                $this->user_id = $field[0];
                $this->fullname = $field[2] . " " . $field[3];
                return TRUE;
            } else {
                return FALSE;
            }
        } else {
            return FALSE;
        }
    }
}